/**
 * 
 */
package idv.takeshi.software.productline.bookstore.application.service.spring;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import idv.takeshi.software.productline.bookstore.application.service.BrowseBooksService;
import idv.takeshi.software.productline.bookstore.domain.model.book.Book;
import idv.takeshi.software.productline.bookstore.domain.model.book.BookRepository;
import idv.takeshi.software.productline.bookstore.domain.model.category.BooksPagingService;
import idv.takeshi.software.productline.bookstore.domain.model.category.Category;
import idv.takeshi.software.productline.bookstore.domain.model.category.CategoryRepository;

/**
 * A service implementation for bookstore browsing operation.
 * @author takeshi
 *
 */
@Service("browseBooksService")
@Transactional
public class BrowseBooksServiceImpl implements BrowseBooksService {
	
	@Autowired
	private CategoryRepository categoryRepository;
	
	@Autowired
	private BookRepository bookRepository;
	
	@Autowired
	private BooksPagingService booksPagingService;
	
	/* (non-Javadoc)
	 * @see idv.takeshi.software.productline.bookstore.application.service.BrowseBooksService#getBookById(java.lang.Long)
	 */
	public Book getBookById(Long id){
		return this.bookRepository.findById(id);
	}

	/* (non-Javadoc)
	 * @see idv.takeshi.software.productline.bookstore.application.service.BrowseBooksService#getRootCategory()
	 */
	public Category getRootCategory() {
		return this.categoryRepository.findRoot();
	}

	/* (non-Javadoc)
	 * @see idv.takeshi.software.productline.bookstore.application.service.BrowseBooksService#getBooksByPageNoAndBookAmountPerPage(boolean, int, int, idv.takeshi.software.productline.bookstore.domain.model.category.Category)
	 */
	public Set<Book> getBooksByPageNoAndBookAmountPerPage(
			boolean isBestSellers, int pageNo, int booksAmountPerPage,
			Category category) {
		Set<Book> books = null;
		if(isBestSellers){
			books = this.booksPagingService.getBestSellersByPageNoAndBookAmountPerPage(
					pageNo, booksAmountPerPage, category);
		} else{
			books = this.booksPagingService.getNewestBooksByPageNoAndBookAmountPerPage(
					pageNo, booksAmountPerPage, category);
		}
		return books;
	}

	/* (non-Javadoc)
	 * @see idv.takeshi.software.productline.bookstore.application.service.BrowseBooksService#getBooksByPageNoAndBookAmountPerPage(java.lang.String, int, int)
	 */
	public Set<Book> getBooksByPageNoAndBookAmountPerPage(String keyWord,
			int pageNo, int booksAmountPerPage) {
		return this.booksPagingService.getSearchBooksByPageNoAndBookAmountPerPage(
				pageNo, booksAmountPerPage, keyWord, bookRepository);
	}

	/* (non-Javadoc)
	 * @see idv.takeshi.software.productline.bookstore.application.service.BrowseBooksService#getBooksAmount(java.lang.String)
	 */
	public int getBooksAmount(String keyWord) {
		return this.bookRepository.findBooksAmountByKeyWord(keyWord);
	}

}
